Creating Variables
To create a variable in AppleScript, assign it a value. There are two commands for doing this:With the Set command, list the variable name first, followed by the value you want to assign:
set myName to "Pegi"With the Copy command, list the value first, followed by the variable name:
copy "Pegi" to myNameStatements like these that assign values to variables are called assignment statements.The variable name is a series of characters called an identifier. AppleScript identifiers are not case sensitive--for example, the variables
myname
,myName
, andMYNAME
all represent the same value. The rules for specifying identifiers are listed in "Identifiers" on page 26.You can list an expression in place of a value in an assignment statement. AppleScript evaluates the expression and assigns the resulting value to the variable. For example, the following statement creates a variable called
myNumber
whose value is the integer 17.
set myNumber to 5 + 12You can also assign a reference as the value of a variable. In this case, AppleScript gets the value of the object specified in the reference and assigns
it to the variable. For example, the following statement gets the value of
the first word of the document called Report--a string--and stores it in a variable calledmyWord
.
set myWord to word 1 of document "Report" of application ÿ "Scriptable Text Editor"You can do the same thing with the Copy command:
copy word 1 of document "Report" of application ÿ "Scriptable Text Editor" to myWordThe results of the two types of assignment statements are the same in all
cases except when the value being assigned is a list, record, or script object.
The Copy command makes a new copy of the list, record, or script object,
and the Set command creates a variable that shares data with the original list, record, or script object. For more information, refer to "Data Sharing" on page 154.